package com.ys.service.eKnowAi.impl;

import com.ys.entity.eKnowAi.EKnowTimerSet;
import com.ys.mapper.eKnowAi.EKnowAiDiySqlMapper;
import com.ys.mapper.eKnowAi.EKnowTimerDiySqlMapper;
import com.ys.mapper.eKnowAi.EKnowTimerSetMapper;
import com.ys.service.eKnowAi.EKnowTimerSetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("eKnowTimerSetService")
public class EKnowTimerSetServiceImpl implements EKnowTimerSetService {

	@Autowired
	private EKnowTimerSetMapper eKnowTimerSetMapper;

	@Autowired
	private EKnowTimerDiySqlMapper eKnowTimerDiySqlMapper;

	@Autowired
	private EKnowAiDiySqlMapper eKnowAiDiySqlMapper;


	@Override
	public int add(EKnowTimerSet eKnowTimerSet) {
		eKnowTimerSet.setStatus(1);
		int result = eKnowTimerSetMapper.insert(eKnowTimerSet);
		return result;
	}


	@Override
	public int update(EKnowTimerSet eKnowTimerSet) {
		eKnowTimerSet.setStatus(1);
		int result = eKnowTimerSetMapper.update(eKnowTimerSet);
		return result;
	}

	//统计物流拼票信息
	@Override
	public List<Map<String, Object>> countLogisticsTicket(){
		List<Map<String, Object>> result = eKnowTimerDiySqlMapper.countLogisticsTicket();
		return result;
	}

	//统计物流拼票中物流方式的信息
	@Override
	public List<Map<String, Object>> countLogisticsTicketGroupByName(){
		List<Map<String, Object>> result = eKnowTimerDiySqlMapper.countLogisticsTicketGroupByName();
		return result;
	}


	//根据标识获取知识库的分块信息
	@Override
	public List<Map<String, Object>> getKnowLedgeListSectionIdByInfoMark(String infoMark){
		List<Map<String, Object>> result = eKnowAiDiySqlMapper.getKnowLedgeListSectionIdByInfoMark(infoMark);
		return result;
	}


	//整理统计后的信息
	@Override
	public List<Map<String, Object>> getLogisticsTicketByCount(List<Map<String, Object>> countLogisticsTicketGroupByName){
		String timeKey = "logisticsTicket";
		//整理统计后的信息
		List<Map<String, Object>> countList = new ArrayList<>();

		//新的统计信息
		List<Map<String, Object>> countLogisticsTicket = eKnowTimerDiySqlMapper.countLogisticsTicket();

		//将物流方式按照不同的国家进行排序
		List<Map<String, Object>> newLogisticsTicketGroup  = setLogisticsTicketGroupByCountry(countLogisticsTicketGroupByName);
		if(countLogisticsTicket!=null && countLogisticsTicket.size()>0 && newLogisticsTicketGroup!=null && newLogisticsTicketGroup.size()>0){
			List<String> countryList = setCountryList();

			//遍历国家
			for(String countryItem:countryList){
				List<Map<String, Object>> ticketGroupByCountry = newLogisticsTicketGroup.stream()
						.filter(item->item.get("countryName").toString().equals(countryItem))
						.collect(Collectors.toList());

				int informationMaxLength = 300;//一段质量最长的字数
				if(ticketGroupByCountry.size()>0){

					String information = "";
					//遍历国家对应的物流方式
					for(int i=0;i<ticketGroupByCountry.size();i++){
						String nameLogsticsWay = ticketGroupByCountry.get(i).get("nameLogsticsWay").toString();
						//根据物流方式获取对应的统计数据
						List<Map<String, Object>> countLogisticsTicketByWay = countLogisticsTicket.stream().filter(item->item.get("nameLogsticsWay").toString().equals(nameLogsticsWay)).collect(Collectors.toList());
						if(countLogisticsTicketByWay.size()>0){
							if(!"".equals(information)){
								String informationNew1 = information +  "\n物流拼票中，物流方式为“"+nameLogsticsWay+"”各个仓库的拼单情况为：";
								if(informationNew1.length()>informationMaxLength){
									Map<String, Object> countMsg = new HashMap<>();
									countMsg.put("information",information);
									countMsg.put("sectionId",timeKey+"_"+countryItem);
									countList.add(countMsg);
									information = "物流拼票中，物流方式为“"+nameLogsticsWay+"”各个仓库的拼单情况为：";
								}else {
									information = informationNew1;
								}
							}

							//遍历物流方式对应的统计数据
							for(int j=0;j<countLogisticsTicketByWay.size();j++) {
								if(countLogisticsTicketByWay.get(j).get("destinationFulfillmentCenterId")!=null){
									String infoMsg = "\n“"+countLogisticsTicketByWay.get(j).get("destinationFulfillmentCenterId")+"”仓库：" +
											"装箱实重"+countLogisticsTicketByWay.get(j).get("weightRealTotal")+"kg；" +
											"票数"+countLogisticsTicketByWay.get(j).get("dataCount")+"票。";
									String informationNew = information + infoMsg;
									if(informationNew.length()<=informationMaxLength){
										information = informationNew;
									}else {
										Map<String, Object> countMsg = new HashMap<>();
										countMsg.put("information",information);
										countMsg.put("sectionId",timeKey+"_"+countryItem);
										countList.add(countMsg);
										information = "物流拼票中，物流方式为“"+nameLogsticsWay+"”各个仓库的拼单情况为："+infoMsg;
									}
								}
							}
						}
					}
					if(!"".equals(information)){
						Map<String, Object> countMsg = new HashMap<>();
						countMsg.put("information",information);
						countMsg.put("sectionId",timeKey+"_"+countryItem);
						countList.add(countMsg);
					}

				}
			}
		}
		/*旧的方案，按物流方式进行分段；新的改成按各个物流方式所在的国家进行分段
		if(countLogisticsTicket!=null && countLogisticsTicket.size()>0 && countLogisticsTicketGroupByName!=null && countLogisticsTicketGroupByName.size()>0){

			for(int i=0;i<countLogisticsTicketGroupByName.size();i++){
				String nameLogsticsWay = countLogisticsTicketGroupByName.get(i).get("nameLogsticsWay").toString();
				List<Map<String, Object>> countLogisticsTicketByWay = countLogisticsTicket.stream().filter(item->item.get("nameLogsticsWay").toString().equals(nameLogsticsWay)).collect(Collectors.toList());
				if(countLogisticsTicketByWay!=null && countLogisticsTicketByWay.size()>0){
					int num = 9;
					for(int j=0;j<countLogisticsTicketByWay.size()+num;j=j+num) {
						if(countLogisticsTicketByWay!=null){
							Map<String, Object> countMsg = new HashMap<>();
							List<Map<String, Object>> newlist = countLogisticsTicketByWay.stream().skip(j).limit(num).collect(Collectors.toList());
							if(newlist!=null && newlist.size()>0){
								String information = "物流拼票中，物流方式为“"+nameLogsticsWay+"”各个仓库的拼单情况为：";
								for(Map<String, Object> newItem:newlist) {
									if(newItem!=null && newItem.get("destinationFulfillmentCenterId")!=null){
										information = information + "\n“"+newItem.get("destinationFulfillmentCenterId")+"”仓库：装箱实重"+newItem.get("weightRealTotal")+"kg；票数"+newItem.get("dataCount")+"票。";
									}
								}
								countMsg.put("information",information);
								countMsg.put("sectionId",timeKey+"_"+nameLogsticsWay);
								countList.add(countMsg);
							}
						}
					}
				}
			}
		}

		 */
		return countList;
	}

	//将物流方式按照不同的国家进行排序
	public List<Map<String, Object>> setLogisticsTicketGroupByCountry(List<Map<String, Object>> countLogisticsTicketGroupByName){
		List<Map<String, Object>> newLogisticsTicketGroup = new ArrayList<>();
		List<String> countryList = setCountryList();
		for(String countryItem:countryList){
			countLogisticsTicketGroupByName.stream().forEach(item->{
				if(item.get("nameLogsticsWay").toString().contains(countryItem)){
					item.put("countryName",countryItem);
					newLogisticsTicketGroup.add(item);
				}
			});
		}
		//剩下
		for(Map<String, Object> item:countLogisticsTicketGroupByName){
			boolean isAdd = false;
			for(Map<String, Object> newitem:newLogisticsTicketGroup){
				if(item.get("nameLogsticsWay").toString().equals(newitem.get("nameLogsticsWay").toString())){
					isAdd = true;
				}
			}
			if(!isAdd){
				item.put("countryName","其他");
				newLogisticsTicketGroup.add(item);
			}
		}
		return newLogisticsTicketGroup;
	}

	//统计物流拼票中物流方式的信息，根据国家排序
	@Override
	public List<Map<String, Object>> countLogisticsTicketGroupByCountry(){
		List<Map<String, Object>> result = setLogisticsTicketGroupByCountry(eKnowTimerDiySqlMapper.countLogisticsTicketGroupByName());
		return result;
	}
	//获取常用国家数据
	@Override
	public List<String> getCountryList(){
		return setCountryList();
	}

	//设置常用国家数据
	public List<String> setCountryList(){
		List<String> countryList = new ArrayList<>();
		countryList.add("美国");
		countryList.add("英国");
		countryList.add("德国");
		countryList.add("法国");

		return countryList;
	}
}
